package com.osgix.demo.mvc.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.osgix.common.mvc.AbsMultiActionController;
import com.osgix.demo.pojo.UserInfo;

@Controller
public class JsonController extends AbsMultiActionController {

	@RequestMapping(value = "/demo/user", method = RequestMethod.POST)
	@ResponseBody
	public UserInfo getUsers(@RequestBody UserInfo user) {
		return user;
	}

	@RequestMapping(value = "/demo/users", method = RequestMethod.POST)
	public List<UserInfo> getUsers(@RequestBody List<UserInfo> users) {
		UserInfo ui = new UserInfo();
		ui.setUserName("userName333");
		ui.setUserId(333333L);
		users.add(ui);
		return users;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub

	}

	protected void outPutJson(String jsonStr, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		response.setContentType("application/json;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		PrintWriter out = response.getWriter();
		out.print(jsonStr);
		out.close();
	}

}
